package com.chen.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.controller.CommonController;
import com.chen.dto.SetmealDto;
import com.chen.entity.Dish;
import com.chen.entity.Setmeal;
import com.chen.entity.SetmealDish;
import com.chen.exception.StatusException;
import com.chen.mapper.SetmealMapper;
import com.chen.service.DishService;
import com.chen.service.SetmealDishService;
import com.chen.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CommonController commonController;
    @Autowired
    private DishService dishService;

    @Override
    public Page<SetmealDto> setmealpage(int page, int pageSize, String name) {
        int start = (page - 1) * pageSize;
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotBlank(name), Setmeal::getName, name);
        int count = count(lqw);
        List<SetmealDto> setmealDtos;
        if (StringUtils.isNotBlank(name)) {
            setmealDtos = setmealMapper.selectPageWithName(start, pageSize, name);
        } else {
            setmealDtos = setmealMapper.selectPage(start, pageSize);
        }
        Page<SetmealDto> setmealPage = new Page<>();
        setmealPage.setRecords(setmealDtos);
        setmealPage.setTotal(count);
        return setmealPage;
    }

    @Override
    public void add(SetmealDto setmealDto) {
        save(setmealDto);
        List<SetmealDish> smealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish smealDish : smealDishes) {
            smealDish.setSetmealId(setmealDto.getId());
        }
        setmealDishService.saveBatch(smealDishes);
    }

    @Override
    public SetmealDto findOne(long id) {
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Setmeal::getId, id);
        Setmeal one = getOne(lqw);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(one, setmealDto);
        LambdaQueryWrapper<SetmealDish> lqw2 = new LambdaQueryWrapper<>();
        lqw2.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> list = setmealDishService.list(lqw2);
        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

    @Override
    public void upDate(SetmealDto setmealDto) {
        updateById(setmealDto);
        Long id = setmealDto.getId();
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId, id);
        setmealDishService.remove(lqw);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(id);
        }
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public void status(long[] ids, int status) {
        Setmeal setmeal = new Setmeal();
        for (long id : ids) {
            setmeal.setId(id);
            setmeal.setStatus(status);
            updateById(setmeal);
        }
    }

    @Override
    public void delt(List<Long> ids) {
        for (Long id : ids) {
            SetmealDto stmealDto = findOne(id);
            if (stmealDto.getStatus() == 1) {
                throw new StatusException("套餐在售,请先停售");
            }
            removeByIds(ids);
            LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
            lqw.eq(SetmealDish::getSetmealId, id);
            setmealDishService.remove(lqw);
            commonController.deleteimg(stmealDto.getImage());
        }
    }

    @Override
    public List<Setmeal> findByid(Long categoryId, Integer status) {
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Setmeal::getCategoryId, categoryId);
        lqw.eq(Setmeal::getStatus, status);
        return setmealMapper.selectList(lqw);
    }

    @Override
    public List<SetmealDish> get(Long setmealId) {
        LambdaQueryWrapper<SetmealDish> lqw=new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,setmealId);
        List<SetmealDish> list = setmealDishService.list(lqw);
        for (SetmealDish setmealDish : list) {
            Long id = setmealDish.getDishId();
            Dish dish = dishService.getById(id);
            String image = dish.getImage();
            setmealDish.setImage(image);
        }
        return list;
    }
}